package com.macaupass.sdkDemo;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.jetbrains.annotations.Nullable;


/**
 * Created by runxian.yang on 2018/11/7.
 * 中間提示的提示dialogfragment
 *
 * @author runxian.yang
 */

public class TipDialogFragment extends DialogFragment implements View.OnClickListener {
    private Activity mContext;
    private Dialog mDialog;
    private Button btnConfirm;  //確認按鈕
    private Button btnCancel;   //取消
    private TextView tvTipContent; //提示內容view
    private String tipContent = ""; //提示內容設置
    private boolean showCancelBtn = false;//是否顯示取消按鈕
    private boolean setCancelDirectionRight = false;
    private RelativeLayout rlLine; //線
    private String Confirm = "";
    private TextView tv_title;
    private boolean showTitle = true;

    /**
     * 設置提示內容
     *
     * @param content
     */
    public void setTvTipsContent(String content) {
        tipContent = content;

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this.getActivity();
    }

    public static TipDialogFragment newInstance() {
        Bundle args = new Bundle();
        TipDialogFragment fragment = new TipDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //设置背景透明
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Dialog dialog = new Dialog(mContext, R.style.MyDialogFragment);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        btnConfirm = view.findViewById(R.id.button_confirm);
        tv_title = view.findViewById(R.id.tv_title);
        tvTipContent = view.findViewById(R.id.tv_tips_content);
        if (!TextUtils.isEmpty(tipContent)) {
            tvTipContent.setText(tipContent);
        }
        if (showCancelBtn) {
            btnCancel = view.findViewById(R.id.button_cancel);
            rlLine = view.findViewById(R.id.rl_tip_line);
            rlLine.setVisibility(View.VISIBLE);
            btnCancel.setVisibility(View.VISIBLE);
            btnCancel.setOnClickListener(this);
        }
        if (!TextUtils.isEmpty(Confirm)) {
            btnConfirm.setText(Confirm);
        }
        btnConfirm.setOnClickListener(this);
        dialog.setContentView(view);
        this.mDialog = dialog;
        dialog.setCanceledOnTouchOutside(true);
        return dialog;
    }


    public void setConfirm(String confirm) {
        Confirm = confirm;
    }

    @Override
    public void onStart() {
        super.onStart();
        //得到dialog对应的window
        Window window = getDialog().getWindow();
        if (window != null) {
            //得到LayoutParams
            WindowManager.LayoutParams params = window.getAttributes();
            //修改gravity
            params.gravity = Gravity.CENTER;
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(params);
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_confirm:
                if (onClickListener != null) {
                    onClickListener.onActionDialogResult(0);
                }
                mDialog.dismiss();
                break;
            case R.id.button_cancel:
                if (onClickListener != null) {
                    onClickListener.onActionDialogResult(1);
                }
                mDialog.dismiss();
                break;
            default:
                break;
        }
    }

    public void hideTitle() {
        showTitle = false;

    }
    public interface IActionDialogResultListener {
        /**
         * 回調接口方法
         * @param action 點擊的action是什麼
         */
        void onActionDialogResult(int action);
    }
    private IActionDialogResultListener onClickListener;

    public void registerOnClickListener(IActionDialogResultListener listener) {
        this.onClickListener = listener;
    }

    public void unRegisterOnClickListener() {
        this.onClickListener = null;
    }

    public void setShowCancelBtn(boolean showCancelBtn) {
        this.showCancelBtn = showCancelBtn;
    }

    public void setSetCancelDirectionRight(boolean setCancelDirectionRight) {
        this.setCancelDirectionRight = setCancelDirectionRight;
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.add(this, tag);
        fragmentTransaction.commitAllowingStateLoss();
    }
}
